package org.millioncall.yueyoga.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.TreeNode;
import org.millioncall.yueyoga.admin.service.TreeNodeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ExpressionAction extends BaseAction {
	
	private TreeNodeService treeNodeService;
	
	public TreeNodeService getTreeNodeService() {
		return treeNodeService;
	}

	public void setTreeNodeService(TreeNodeService treeNodeService) {
		this.treeNodeService = treeNodeService;
	}

	/**
	 * 跳转到高级查询页面
	 * */
	@RequestMapping(value="toExpression.jhtml")
	public String toAdvancedSearch(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		TreeNode treeSone = treeNodeService.findAllTreeById("sourceMember");
		String json = treeNodeService.toZTreeJsonString(treeSone);
		model.put("tree", json);
		return "advance_search";
	}
	
	/** 查询不同的树 */
	@RequestMapping(value="ajaxFindTreeByType.jhtml")
	public void findByType(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		String type = request.getParameter("type");
		
		if (StringUtils.isBlank(type)) {
			msg = "{'success':false, 'msg':'类型不能为空！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		TreeNode tree = this.treeNodeService.findAllTreeById(type);
		String json = this.treeNodeService.toZTreeJsonString(tree);
		
		if (tree != null) {
			msg = "{'success':true, 'items':" + json + "}";
		} else {
			msg = "{'success':false, 'msg':'未找到数据！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
}
